package com.beiding.controller;

import com.beiding.pojo.Evaluation;
import com.beiding.pojo.Room;
import com.beiding.service.EvaluationService;
import com.beiding.service.RoomStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@RestController
@RequestMapping("/feign")
public class FeignController {


    private EvaluationService evaluationService;

    private RoomStorageService storageService;

    @Autowired
    public void setStorageService(RoomStorageService storageService) {
        this.storageService = storageService;
    }

    @Autowired
    public void setEvaluationService(EvaluationService evaluationService) {
        this.evaluationService = evaluationService;
    }

    //评价操作,返回评价的id
    @PostMapping("/evaluate")
    String evaluate(@RequestBody Evaluation evaluation) {
        return evaluationService.evaluate(evaluation);
    }


    @PostMapping("/update")
    Room update(Principal principal, @RequestBody Room room) {
        return storageService.update(principal, room);
    }

    @PostMapping("/remove")
    void remove(Principal principal, @RequestParam String id) {
        storageService.remove(principal, id);
    }

    @PostMapping("/put")
    Room put(Principal principal, @RequestBody Room room) {

        room.init();
        room.setHolderId(principal.getName());

        return storageService.save(room);
    }

    @GetMapping("/get")
    Room roomDetails(@RequestParam String id) {
        return storageService.getRoomDetails(id);
    }

    //占用房源
    @PostMapping("/occupy")
    void occupy(String id, String theUser) {
        storageService.occupy(id, theUser);
    }

    //释放对房源占用
    @PostMapping(value = "/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    void release(String id) {
        storageService.release(id);
    }


}
